//
//  CLLWBTtitleButton.swift
//  CLWeiNo
//
//  Created by DDC on 2017/10/13.
//  Copyright © 2017年 DDC. All rights reserved.
//

import UIKit

class CLLWBTtitleButton: UIButton {


    init(title:String?) {
        super.init(frame: CGRect())
        
        // 设置title的属性
        if title == nil {
            setTitle("首页", for: [])
        } else {
            
            // 设置标题按钮
            setTitle(title!, for: [])
            
            setImage(UIImage(named:"navigationbar_arrow_down"), for: [])
            setImage(UIImage(named:"navigationbar_arrow_up"), for: .selected)
        }
        
        titleLabel?.font = UIFont.systemFont(ofSize: 16)
        setTitleColor(UIColor.darkGray, for: [])
        
        sizeToFit()
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置属性的布局
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // 属性控件titleblabel,imageView
        guard let titleLabel = titleLabel,
              let imageView = imageView
        else {
            return
        }
        // 将titlelabel 偏移imageView的宽度
        // 同理imageview一样
        /**
         FIXME: 有一个bug  当第一次加载页面的时候会发现按钮的图片离标题很远，点击之后就会还原.重新布置按钮的控件是rect
         */
        // titleLabel.frame = titleLabel.frame.offsetBy(dx:-imageView.bounds.width, dy: 0)
        
        // imageView.frame = imageView.frame.offsetBy(dx: titleLabel.bounds.width + 10, dy: 0)
        
        titleLabel.frame = CGRect(x: 0, y: 0, width: titleLabel.bounds.width
            , height: titleLabel.bounds.height)
        imageView.frame = CGRect(x: titleLabel.bounds.width + 5, y: 7, width: imageView.bounds.width, height: imageView.bounds.height)
    }
    
    
    
}
